﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataStandard
{
    /// <summary>
    /// 数据项
    /// </summary>
    public class DataItem
    {

        public string ParentName { get; set; }
        public int Functioncode {  get; set; }
        public DataItemDescription Description;

        public DataItem() { }

        /// <summary>
        /// 数据值
        /// </summary>
        public object Value => Tools.DataValue.GetRealDataValue(Description);
        public List<bool> BitsBool => Tools.DataValue.GetBoolBits(Description);
        public List<int> BitsValue => Tools.DataValue.GetValueBits(Description);


        public byte[] WriteValueConvertToByteS(object value)
        {
            return Tools.DataValue.WriteRealValueConvertToByteS(value, Description);
        }
        

        public bool GetBitBool_By_Index(int index)
        {
            return Tools.DataValue.GetBitBool_By_Index(index,Description);
        }

        public int GetBitValue_By_Index(int index)
        {
            return Tools.DataValue.GetBitValue_By_Index(index,Description);
        }

    }


    /// <summary>
    /// 数据描述
    /// </summary>
    public class DataItemDescription
    {
        public DataItemDescription() { }

        /// <summary>
        /// 数据名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 单位 比如： kW、m/s、A、V ， 等……
        /// </summary>
        public string Unit { get; set; }
        /// <summary>
        /// 起始地址
        /// </summary>
        public int StartAddress {  get; set; }
        /// <summary>
        /// 数据类型：INT16/UINT16/INT32/UINT32/BOOL/FLOAT
        /// </summary>
        public string Type { get; set; }
        public int ByteLength => Tools.DataTypeWithByteLength.GetByteLength(Type);

        public byte UnitIdentifier {  get; set; }

        /// <summary>
        /// 高低序位：12、21、1234、4321、2143、3412，够用了。
        /// </summary>
        public string RegisterOrder { get; set; }
        /// <summary>
        /// 位置，如果是-1，就不管。
        /// 正常数值范围：0-15
        /// 一般来说，这个值不是-1的情况下，Type基本会是INT16或者UINT16。
        /// 如果不是，就按照这个给取值。
        /// </summary>
        public int BitIndex { get; set; } = -1;
        /// <summary>
        /// 倍数
        /// </summary>
        public double Multiple { get; set; } = 1;
        /// <summary>
        /// 数据偏移量
        /// </summary>
        public double Offset { get; set; } = 0;
        /// <summary>
        /// 最新数据字节
        /// </summary>
        public byte[] NewReceivedBytes { get; set; }
        /// <summary>
        /// 最新接收数据的时间
        /// </summary>
        public DateTime NewReceivedDateTime { get; set; }
        /// <summary>
        /// 上次接收数据的字节
        /// </summary>
        public byte[] LastReceivedBytes { get; set; }
        /// <summary>
        /// 上次接收数据的时间
        /// </summary>
        public DateTime LastReceivedDateTime { get; set; }
        /// <summary>
        /// 设定间隔时间
        /// </summary>
        public int SettingTimer { get; set; } = 1000;
    }
}
